﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LockCrypt.Core {
    public class WildcardValue {
        public enum WildcardTypes {
            Contains,
            StartsWith,
            EndsWith
        }

        public string Value { get; set; }
        public WildcardTypes WildcardType { get; set; }

        public WildcardValue(string value, WildcardTypes type) {
            this.Value = value;
            this.WildcardType = type;
        }

        /// <summary>
        /// Determines whether a string matches this wildcard using culture and case invariant comparison.
        /// </summary>
        /// <param name="valueToMatch">The string to match.</param>
        /// <returns><c>true</c> if this wildcard matches the string, otherwise <c>false</c>.</returns>
        public bool Matches(string valueToMatch) {
            bool matches;
            switch (WildcardType) {
                case WildcardTypes.StartsWith:
                    matches = valueToMatch.StartsWith(Value, StringComparison.InvariantCultureIgnoreCase);
                    break;
                case WildcardTypes.EndsWith:
                    matches = valueToMatch.EndsWith(Value, StringComparison.InvariantCultureIgnoreCase);
                    break;
                case WildcardTypes.Contains:
                default:
                    matches = valueToMatch.IndexOf(Value, StringComparison.InvariantCultureIgnoreCase) >= 0;
                    break;
            }
            return matches;
        }

        /// <summary>
        /// Clones this WildcardValue.
        /// </summary>
        /// <returns>A clone of this WildcardValue</returns>
        public WildcardValue Clone() {
            return new WildcardValue(this.Value, this.WildcardType);
        }
    }
}
